env.setErrorMessageBoxEnabled(true)

-- Zone table
-- 保存了炮击地点的 table
local ARTYZONEs = {
  FARP = ZONE:New("ARTYZONE_FARP"),
  Khaimah = ZONE:New("ARTYZONE_Khaimah"),
  Dubai = ZONE:New("ARTYZONE_Dubai"),
}

-- init base flag
-- 定义了机场初始阵营的 flag
local Flag_Khaimah = true
local Flag_Dubai = true

-- ARTY scheduler table
-- 保存了炮击计划的 table
ARTY_SCHEDULERS = {
  nil,
  nil,
  nil
}

-- 火炮开火方法
local function artyFiring(para)
  local tempgroup, artyzone, place
  local alivegroup = nil

  -- init parameters
  -- para是一个table，有两个元素:
  -- group: 传递进来的MOOSE_GROUP对象，用于给指定的火炮群组发布任务
  -- artyzone: 传递进来的MOOSE_ZONE对象，用于给指定的火炮群组指定攻击地点。
  if para then
    tempgroup = para.group
    artyzone = para.artyzone
    place = para.place

    if tempgroup then
      -- check group
      -- tempgroup 是由生成火炮群组时传递进来的，而这个方法会在SCHEDULER调用，为了避免在调用时火炮被击毁而找不到群组，我们需要先查找该群组是否存活
      if GROUP:FindByName(tempgroup.GroupName) then
        alivegroup = GROUP:FindByName(tempgroup.GroupName)
      end
    end
  end

  -- 只有找到了存活的群组才会推送炮击任务，如果群组阵亡则不推送。
  if alivegroup then
    if not artyzone then
      MessageToAll("ZONE IS NIL", 10, "DEBUG")
      return
    end
    local ARTYVEC2 = artyzone:GetVec2()
    local target = {}
    target.x = ARTYVEC2.x
    target.y = ARTYVEC2.y
    target.radius = 800
    target.expendQty = 10
    target.expendQtyEnabled = true

    local fire = {
      id = "FireAtPoint",
      params = target
    }
    Group.getByName(alivegroup.GroupName):getController():pushTask(fire)
    MessageToAll("注意：我们发现敌方火炮正在活动。 地点: " .. place, 10, "格里芬战情通报")
  end
end


-- SPAWNS
-- -IRONBLOOD Base
IRONBLOODBASE_Khaimah = SPAWN:New("IRONBLOOD GROUND BASE #002")
IRONBLOODBASE_Dubai = SPAWN:New("IRONBLOOD GROUND BASE #003")
IRONBLOODBASE_Fujairah = SPAWN:New("IRONBLOOD GROUND BASE #013")

-- fighter
IRONBLOOD_Su33 = SPAWN:New("IRONBLOOD NAVY Fighter"):InitLimit(2, 0):SpawnScheduled(1000, 0.3):InitRepeat()
IRONBLOOD_Su33:SpawnScheduleStart()
IRONBLOOD_Su33:HandleEvent(EVENTS.Dead)
function IRONBLOOD_Su33:OnEventDead(EventData)
  if EventData.IniUnitName == "IRONBLOOD CVBattleGroup" then
    self:SpawnScheduleStop()
    self = nil
  end
end

-- ARTY table
-- 保存了所有红方火炮的SPAWN对象
IRONBLOODARTYs = {
  -- 1 FIREATPOINT FARP
  SPAWN:New("IRONBLOOD Arty"):InitLimit(5, 0)
      :OnSpawnGroup(function(group)
        if not ARTY_SCHEDULERS[1] then
          local temptable = { group = group, artyzone = ARTYZONEs.FARP, place = "哈伊马角机场" }
          local temp_scheduler = {}
          temp_scheduler.object, temp_scheduler.id = SCHEDULER:New(nil, artyFiring, { temptable }, 5, 480)
          ARTY_SCHEDULERS[1] = temp_scheduler
        else
          ARTY_SCHEDULERS[1].object:Stop(ARTY_SCHEDULERS[1].id)
          ARTY_SCHEDULERS[1].object:Remove(ARTY_SCHEDULERS[1].id)
          ARTY_SCHEDULERS[1] = {}
        end
      end)
      :SpawnScheduled(1058, 0.3):SpawnScheduleStop(),

  -- 2 FIREATPOINT Khaimah-2
  SPAWN:New("IRONBLOOD Arty-2"):InitLimit(5, 0)
      :OnSpawnGroup(function(group)
        if not ARTY_SCHEDULERS[2] then
          local temptable = { group = group, artyzone = ARTYZONEs.Khaimah, place = "哈伊马角北部铁血基地" }
          local temp_scheduler = {}
          temp_scheduler.object, temp_scheduler.id = SCHEDULER:New(nil, artyFiring, { temptable }, 5, 480)

          ARTY_SCHEDULERS[2] = temp_scheduler
        else
          ARTY_SCHEDULERS[2].object:Stop(ARTY_SCHEDULERS[2].id)
          ARTY_SCHEDULERS[2].object:Remove(ARTY_SCHEDULERS[2].id)
          ARTY_SCHEDULERS[2] = {}
        end
      end)
      :SpawnScheduled(1058, 0.3):SpawnScheduleStop(),

  -- 3 FIREATPOINT DUBAI
  SPAWN:New("IRONBLOOD Arty-1"):InitLimit(5, 0)
      :OnSpawnGroup(function(group)
        if not ARTY_SCHEDULERS[3] then
          local temptable = { group = group, artyzone = ARTYZONEs.Dubai, place = "迪拜东部铁血基地" }
          local temp_scheduler = {}
          temp_scheduler.object, temp_scheduler.id = SCHEDULER:New(nil, artyFiring, { temptable }, 5, 480)

          ARTY_SCHEDULERS[3] = temp_scheduler
        else
          ARTY_SCHEDULERS[3].object:Stop(ARTY_SCHEDULERS[3].id)
          ARTY_SCHEDULERS[3].object:Remove(ARTY_SCHEDULERS[3].id)
          ARTY_SCHEDULERS[3] = {}
        end
      end)
      :SpawnScheduled(1058, 0.3):SpawnScheduleStop(),
}

-- -GIFFIN
GRIFFINBASE_Khaimah = SPAWN:New("GRIFFIN AIRBASE DEFEND #001")
GRIFFINBASE_Dubai = SPAWN:New("GRIFFIN AIRBASE DEFEND #002")
GRIFFINBASE_Fujairah = SPAWN:New("GRIFFIN AIRBASE DEFEND #003")
GRIFFINFARP_SUPPORT = SPAWN:New("IRONBLOOD FARP SUPPORT-1")

-- IRONBLOOD ATTACKER
IRONBLOOD_ATTACKER = SPAWN:New("IRONBLOOD Attacker #001"):InitLimit(2, 0)
    :OnSpawnGroup(function(group)
      MessageToAll("格里芬早期预警: 注意，我们侦测到哈伊马角东部有敌方攻击机群起飞，请注意拦截",
        10, "格里芬空情通报")
      SOUND_HOSTILE_02:ToAll()
    end)
    :InitRepeat()
    :InitAirbase(AIRBASE.PersianGulf.Fujairah_Intl)
    :SpawnScheduled(2280, 0.5):SpawnScheduleStop()
-- IRONBLOOD Bomber
IRONBLOOD_ATTACKER_1 = SPAWN:New("IRONBLOOD Attacker #002"):InitLimit(2, 0)
    :OnSpawnGroup(function(group)
      MessageToAll("格里芬早期预警: 注意，我们侦测到哈伊马角东部有敌方轰炸机群起飞，请注意拦截",
        10, "格里芬空情通报")
      SOUND_HOSTILE_02:ToAll()
    end)
    :InitRepeat()
    :SpawnScheduled(2280, 0.5):SpawnScheduleStop()
-- IRONBLOOD ATTACKHELO
IRONBLOOD_ATTACKHELO = SPAWN:New("IRONBLOOD ATTKHELO-1"):InitLimit(2, 0)
    :OnSpawnGroup(function(group)
      MessageToAll("格里芬早期预警: 注意，我们侦测到哈伊马角东部有直升机起飞，请注意拦截",
        10, "格里芬空情通报")
      SOUND_HOSTILE_01:ToAll()
    end)
    :InitRepeat()
    :SpawnScheduled(1800, 0.1):SpawnScheduleStop()



-- Khaimah RED GROUND ATTACK SPAWN
ZONETABLE_Khaimah = {}
local ZONESET_AIRBASE_ATTACK = SET_ZONE:New():FilterPrefixes("IRONBLOOD GROUND #001"):FilterStart()
ZONESET_AIRBASE_ATTACK:ForEachZone(function(zone)
  table.insert(ZONETABLE_Khaimah, zone)
end)
IRONBLOOD_AIRBASE_ATTACK_Khaimah = SPAWN:New("IRONBLOOD AIBASE ATTACK #001"):InitRandomizeZones(
      ZONETABLE_Khaimah)
    :OnSpawnGroup(function(group)
      MessageToAll("格里芬早期预警: 注意，我们侦测到哈伊马角机场附近有铁血地面部队正在集结，请注意防御",
        10, "格里芬空情通报")
      SOUND_HOSTILE_01:ToAll()
    end)
    :SpawnScheduled(2000, 0.1):SpawnScheduleStop()

-- Dubai RED GROUND ATTACK SPAWN
ZONETABLE_Dubai = {}
local ZONESET_AIRBASE_ATTACK_Dubai = SET_ZONE:New():FilterPrefixes("IRONBLOOD GROUND #002"):FilterStart()
ZONESET_AIRBASE_ATTACK_Dubai:ForEachZone(function(zone)
  table.insert(ZONETABLE_Dubai, zone)
end)
IRONBLOOD_AIRBASE_ATTACK_Dubai = SPAWN:New("IRONBLOOD AIBASE ATTACK #002"):InitRandomizeZones(
      ZONETABLE_Dubai)
    :OnSpawnGroup(function(group)
      MessageToAll("格里芬早期预警: 注意，我们侦测到迪拜国际机场附近有铁血地面部队正在集结，请注意防御",
        10, "格里芬空情通报")
      SOUND_HOSTILE_01:ToAll()
    end)
    :SpawnScheduled(2000, 0.15):SpawnScheduleStop()

-- STATICS
-- IRONBLOOOD 北方仓库
STATIC_01 = STATIC:FindByName("IRONBLOOD AMMO-1-1")
STATIC_01:HandleEvent(EVENTS.Dead)
function STATIC_01:OnEventDead(EventData)
  if EventData.IniUnitName == "IRONBLOOD AMMO-1-1" then
    MessageToAll("格里芬战情通报: 哈伊马角北方的火炮阵地已经被清除", 10, "格里芬战情通报")
    if self then
      self:Destroy()
      self:UnHandleEvent(EVENTS.Dead)
    else
      MessageToAll("STATIC_01 is nil", 10, "DEBUG")
    end
    if IRONBLOODARTYs[2] then
      IRONBLOODARTYs[2]:SpawnScheduleStop()
      IRONBLOODARTYs[2] = nil
      MessageToAll("格里芬战情通报：哈伊马角北方的铁血火炮阵地已经被摧毁,火炮威胁清除。",
      10, "格里芬战情通报")
    end
    -- 删除火炮群组和计划
    STATIC_01 = nil
    if ARTY_SCHEDULERS[2] then
      ARTY_SCHEDULERS[2].object:Stop(ARTY_SCHEDULERS[2].id)
      ARTY_SCHEDULERS[2].object:Remove(ARTY_SCHEDULERS[2].id)
    end
    ARTY_SCHEDULERS[2] = nil
  end
end

-- IRONBLOOOD 南方仓库
STATIC_02 = STATIC:FindByName("IRONBLOOD AMMO-2-1")
STATIC_02:HandleEvent(EVENTS.Dead)
function STATIC_02:OnEventDead(EventData)
  if EventData.IniUnitName == "IRONBLOOD AMMO-2-1" then
    MessageToAll("格里芬战情通报: 迪拜附近的铁血火炮阵地已经被清除", 10, "格里芬战情通报")
    if self then
      self:Destroy()
      self:UnHandleEvent(EVENTS.Dead)
    else
      MessageToAll("STATIC_02 is nil", 10, "DEBUG")
    end

    -- 删除火炮群组和计划
    STATIC_01 = nil
    if ARTY_SCHEDULERS[3] then
      ARTY_SCHEDULERS[3].object:Stop(ARTY_SCHEDULERS[3].id)
      ARTY_SCHEDULERS[3].object:Remove(ARTY_SCHEDULERS[3].id)
    end

    ARTY_SCHEDULERS[3] = nil

    if IRONBLOODARTYs[3] then
      IRONBLOODARTYs[3]:SpawnScheduleStop()
      IRONBLOODARTYs[3] = nil
      MessageToAll("格里芬战情通报：迪拜附近的铁血火炮阵地已经被摧毁,火炮威胁清除。", 10,
      "格里芬战情通报")
    end
  end
end

-- 解放机位
function clientsControl(client, flag)
  local tempName = tostring(client.ClientName)
  USERFLAG:New(tempName):Set(flag["val"])
end

-- 坠毁事件
EVENTHANDLER_UNIT_BLUE_CRASH = EVENTHANDLER:New():HandleEvent(EVENTS.Crash)
function EVENTHANDLER_UNIT_BLUE_CRASH:OnEventCrash(EventData)
  if EventData.IniPlayerName ~= nil then
    local output_msg = "注意，有玩家坠毁，请前往战区协助 推进。"
    MessageToAll(output_msg, 10, "格里芬空中指挥通报")
    --    USERSOUND:New("特工倒下division.ogg"):ToAll()
    SOUND_PLAYER_DOWN:ToAll()
  end
end

-- 起飞事件
EVENTHANDLER_TAKEOFF = EVENTHANDLER:New():HandleEvent(EVENTS.Takeoff)
function EVENTHANDLER_TAKEOFF:OnEventTakeoff(EventData)
  if EventData.IniUnit:GetCoalition() == SIDE.BLUE and EventData.IniPlayerName ~= nil then
    local output_msg = "玩家 " ..
        tostring(EventData.IniPlayerName) .. " 驾驶 " .. tostring(EventData.IniTypeName) .. "加入了战局. 起飞自"
    local placeName = "地面"

    if EventData.Place ~= nil then
      output_msg = output_msg .. tostring(EventData.Place:GetName())
    else
      output_msg = output_msg .. placeName
    end

    MESSAGE:New(output_msg, 10, "格里芬空中指挥通报", true):ToAll()
    SOUND_TAKEOFF:ToAll()
  end
end

-- 降落事件
EVENTHANDLER_LANDING = EVENTHANDLER:New():HandleEvent(EVENTS.Land)
function EVENTHANDLER_LANDING:OnEventLand(EventData)
  if EventData.IniPlayerName ~= nil then
    local output_msg = "玩家" .. tostring(EventData.IniPlayerName) .. "已经退出战局并降落在"
    local placename = "地面"
    if EventData.PlaceName then
      placename = EventData.Place.GetName()
      MESSAGE:New(output_msg .. tostring(placename), 10, "格里芬战局通报", true):ToAll()
    else
      MESSAGE:New(output_msg .. placename, 10, "格里芬战局通报", true):ToAll()
    end
    SOUND_LAND:ToAll()
  end
end

-- ******************铁血FARP占领开始
CaptureZone_IRONBLOODFARP = ZONE:New("IRONBLOODFARPZone")
ZoneCaptureCoalition_IRONBLOODFARP = ZONE_CAPTURE_COALITION:New(CaptureZone_IRONBLOODFARP, SIDE.RED)
ZoneCaptureCoalition_IRONBLOODFARP:__Guard(1)

ZoneCaptureCoalition_IRONBLOODFARP:Start(5, 60)

function ZoneCaptureCoalition_IRONBLOODFARP:onenterGuarded(From, Event, To) -- 铁血FARP占领机场事件
  if From ~= To then
    local tempCoalition = self:GetCoalition()
    if tempCoalition == SIDE.BLUE then
      ZoneCaptureCoalition_IRONBLOODFARP:Smoke(SMOKECOLOR.Blue)
      -- 通报
      MessageToAll("格里芬战情通报:格里芬的部队占领了富查伊拉附近的一处前线燃油补给点",
        10, "格里芬战情通报")
      SOUND_BASECAPTURED:ToAll()

      -- 停止直升机生成
      IRONBLOOD_ATTACKHELO:SpawnScheduleStop()
      IRONBLOOD_ATTACKHELO = nil

      -- 生成FARP补给部队
      GRIFFINFARP_SUPPORT:Spawn()
    elseif tempCoalition == SIDE.RED then
      MessageToAll("格里芬战情通报: 富查伊拉发现附近有一处前线燃油补给点。", 10, "格里芬战情通报")
    end
  end
end

function ZoneCaptureCoalition_IRONBLOODFARP:OnEnterAttacked() -- 通报正在被进攻
  local beattacked = self:GetCoalition()
  if beattacked == SIDE.BLUE then
    MessageToAll("格里芬战情通报: 富查伊拉前线燃油补给点正在被铁血进攻！", 10, "格里芬战情通报")
    SOUND_HOSTILE_01:ToAll()
  else
    MessageToAll("格里芬战情通报: 格里芬地面部队正在进攻富查伊拉前线燃油补给点", 10,
      "格里芬战情通报")
    SOUND_BASEATTACKING:ToAll()
  end
end

function ZoneCaptureCoalition_IRONBLOODFARP:onenterEmpty() --当玩家清除完目标后生成蓝方以占领
  MessageToAll("富查伊拉前线燃油补给点威胁已被清除。正在接管", 10, "格里芬战情通报")
  GRIFFINFARP_SUPPORT:Spawn()
end

-- ******************铁血FARP占领结束

-- ******************富查伊拉机场占领开始
CaptureZone_Fujairah = ZONE:New("FujairahZone")
ZoneCaptureCoalition_Fujairah = ZONE_CAPTURE_COALITION:New(CaptureZone_Fujairah, SIDE.RED)
ZoneCaptureCoalition_Fujairah:__Guard(1)

ZoneCaptureCoalition_Fujairah:Start(5, 60)

function ZoneCaptureCoalition_Fujairah:onenterGuarded(From, Event, To) -- 富查伊拉占领机场事件
  if From ~= To then
    local tempCoalition = self:GetCoalition()
    if tempCoalition == SIDE.BLUE then
      ZoneCaptureCoalition_Fujairah:Smoke(SMOKECOLOR.Blue)
      -- 通报
      MessageToAll("格里芬战情通报: 格里芬的部队占领了富查伊拉国际机场", 10, "格里芬战情通报")
      SOUND_BASECAPTURED:ToAll()


      -- 停止飞机和地面单位生成
      IRONBLOOD_AIRBASE_ATTACK_Dubai:SpawnScheduleStop()
      IRONBLOOD_AIRBASE_ATTACK_Khaimah:SpawnScheduleStop()
      IRONBLOOD_ATTACKER:SpawnScheduleStop()
      IRONBLOOD_ATTACKER_1:SpawnScheduleStop()

      IRONBLOOD_ATTACKER = nil
      IRONBLOOD_ATTACKER_1 = nil
      IRONBLOOD_AIRBASE_ATTACK_Dubai = nil
      IRONBLOOD_AIRBASE_ATTACK_Khaimah = nil

      -- 生成蓝方部队
      if GRIFFINBASE_Fujairah:GetFirstAliveGroup() == nil then
        GRIFFINBASE_Fujairah:ReSpawn()
      end
    elseif tempCoalition == SIDE.RED then
      ZoneCaptureCoalition_Fujairah:Smoke(SMOKECOLOR.Red)
      MessageToAll("格里芬战情通报: 富查伊拉国际机场已经被铁血夺回, 请重新规划进攻以占领机场",
        10, "格里芬战情通报")

      -- 生成红方基地
      if IRONBLOODBASE_Fujairah:GetFirstAliveGroup() == nil then
        IRONBLOODBASE_Fujairah:ReSpawn()
      end
    end
  end
end

function ZoneCaptureCoalition_Fujairah:OnEnterAttacked() -- 通报正在被进攻
  local beattacked = self:GetCoalition()
  if beattacked == SIDE.BLUE then
    MessageToAll("格里芬战情通报: 富查伊拉正在被铁血进攻！")
    SOUND_HOSTILE_01:ToAll()
  else
    MessageToAll("格里芬战情通报: 格里芬地面部队正在进攻富查伊拉。")
    SOUND_BASEATTACKING:ToAll()
  end
end

function ZoneCaptureCoalition_Fujairah:onenterEmpty() --当玩家清除完目标后生成蓝方以占领
  MessageToAll("富查伊拉威胁已被清除。正在接管", 10, "格里芬战情通报")
  GRIFFINBASE_Fujairah:Spawn()
end

-- ******************富查伊拉机场占领结束


-- ******************哈伊马角机场占领开始
-- 哈伊马角机场区域占领
CaptureZone_Khaimah = ZONE:New("KhaimahZone")
ZoneCaptureCoalition_Khaimah = ZONE_CAPTURE_COALITION:New(CaptureZone_Khaimah, SIDE.RED)
ZoneCaptureCoalition_Khaimah:__Guard(1)

ZoneCaptureCoalition_Khaimah:Start(1, 10)

function ZoneCaptureCoalition_Khaimah:onenterGuarded(From, Event, To) -- 哈伊马角占领机场事件
  if From ~= To then
    local tempCoalition = self:GetCoalition()
    if tempCoalition == SIDE.BLUE then
      Flag_Khaimah = true
      -- open clients in khaimah
      clients_Khaimah:ForEachClient(clientsControl, { val = 0 })

      -- 当哈伊马角被占领，哈伊马角的火炮不再重生
      if IRONBLOODARTYs[1] then
        IRONBLOODARTYs[1]:SpawnScheduleStop()
        MessageToAll("格里芬战情通报:铁血哈伊马角的火炮阵地遭受了致命打击。", 10, "格里芬战情通报")
      end

      -- 通报
      MessageToAll("格里芬战情通报: 格里芬的部队占领了哈伊马角机场", 10, "格里芬战情通报")
      SOUND_BASECAPTURED:ToAll()

      -- 攻击机群
      if IRONBLOOD_ATTACKER and IRONBLOOD_ATTACKER_1 then
        IRONBLOOD_ATTACKER:SpawnScheduleStart()
        IRONBLOOD_ATTACKER_1:SpawnScheduleStart()
      end

      -- GROUND ATTACK
      if IRONBLOOD_AIRBASE_ATTACK_Khaimah then
        IRONBLOOD_AIRBASE_ATTACK_Khaimah:SpawnScheduleStart()
      end
      -- HELO
      if IRONBLOOD_ATTACKHELO then
        IRONBLOOD_ATTACKHELO:SpawnScheduleStart()
      end

      -- 生成 GRIFFIN Base
      if GRIFFINBASE_Khaimah:GetFirstAliveGroup() == nil then
        GRIFFINBASE_Khaimah:ReSpawn()
      end

      -- 当哈伊马角被占领，向哈伊马角开火的火炮开始重生
      IRONBLOODARTYs[2]:SpawnScheduleStart()



      if ARTY_SCHEDULERS[1] then
        ARTY_SCHEDULERS[1].object:Stop(ARTY_SCHEDULERS[1].id)
        ARTY_SCHEDULERS[1].object:Remove(ARTY_SCHEDULERS[1].id)
        ARTY_SCHEDULERS[1] = nil
      end
    elseif tempCoalition == SIDE.RED then -- 红方占领事件
      Flag_Khaimah = false
      -- 关闭机场
      clients_Khaimah:ForEachClient(clientsControl, { val = 100 })

      MessageToAll("格里芬战情通报: 哈伊马角已经被铁血夺回, 请重新规划进攻以占领机场", 10,
        "格里芬战情通报")

      IRONBLOODARTYs[1]:SpawnScheduleStart()

      -- ATTACKER & Bomber
      if IRONBLOOD_ATTACKER and IRONBLOOD_ATTACKER_1 then
        IRONBLOOD_ATTACKER:SpawnScheduleStop()
        IRONBLOOD_ATTACKER_1:SpawnScheduleStop()
      end

      -- GROUNDATTACK
      if IRONBLOOD_AIRBASE_ATTACK_Khaimah then
        IRONBLOOD_AIRBASE_ATTACK_Khaimah:SpawnScheduleStop()
      end

      -- HELO
      if IRONBLOOD_ATTACKHELO then
        IRONBLOOD_ATTACKHELO:SpawnScheduleStop()
      end

      -- respawn IRONBLOOD Base
      if IRONBLOODBASE_Khaimah:GetFirstAliveGroup() == nil then
        IRONBLOODBASE_Khaimah:Spawn()
      end

      -- 进攻FARP的火炮开始
      if ARTY_SCHEDULERS[1] then
        ARTY_SCHEDULERS[1].object:Start(ARTY_SCHEDULERS[1].id)
      end

      -- 进攻哈伊马角的火炮结束
      if IRONBLOODARTYs[2] then               -- 检测弹药库状态, 如果还存在，则停止生成并停止炮击计划
        IRONBLOODARTYs[2]:SpawnScheduleStop() -- 停止生成计划
        if ARTY_SCHEDULERS[2] then
          ARTY_SCHEDULERS[2].object:Stop(ARTY_SCHEDULERS[2].id)
        end
      end
    end
  end
end

function ZoneCaptureCoalition_Khaimah:OnEnterAttacked() -- 通报正在被进攻
  local beattacked = self:GetCoalition()
  if beattacked == SIDE.BLUE then
    MessageToAll("格里芬战情通报: 哈伊马角正在被铁血进攻！", 10, "格里芬战情通报")
    SOUND_HOSTILE_01:ToAll()
  else
    MessageToAll("格里芬战情通报: 格里芬地面部队正在进攻哈伊马角。", 10, "格里芬战情通报")
    IRONBLOODARTYs[1]:SpawnScheduleStop()
    SOUND_BASEATTACKING:ToAll()
  end
end

function ZoneCaptureCoalition_Khaimah:onenterEmpty() --当玩家清除完目标后生成蓝方以占领
  if not Flag_Khaimah then
    MessageToAll("格里芬战情通报: 哈伊马角威胁已被清除。正在接管", 10, "格里芬战情通报")
    GRIFFINBASE_Khaimah:Spawn()
  else
    IRONBLOODBASE_Khaimah:Spawn()
  end
end

-- ******************哈伊马角机场占领结束

-- ******************迪拜机场占领开始
CaptureZone_Dubai = ZONE:New("DubaiZone")
ZoneCaptureCoalition_Dubai = ZONE_CAPTURE_COALITION:New(CaptureZone_Dubai, SIDE.RED)
ZoneCaptureCoalition_Dubai:__Guard(1)

ZoneCaptureCoalition_Dubai:Start(5, 30)

function ZoneCaptureCoalition_Dubai:onenterGuarded(From, Event, To) -- 迪拜占领机场事件
  if From ~= To then
    Flag_Dubai = true
    -- 开放机场
    clients_Dubai:ForEachClient(clientsControl, { val = 0 })
    local tempCoalition = self:GetCoalition()
    if tempCoalition == SIDE.BLUE then
      -- 通报
      MessageToAll("格里芬的部队占领了迪拜国际机场", 10, "格里芬战情通报")
      SOUND_BASECAPTURED:ToAll()

      -- 生成红方进攻部队
      if IRONBLOOD_AIRBASE_ATTACK_Dubai then
        IRONBLOOD_AIRBASE_ATTACK_Dubai:SpawnScheduleStart()
      end

      -- 生成蓝方基地
      if GRIFFINBASE_Dubai:GetFirstAliveGroup() == nil then
        GRIFFINBASE_Dubai:ReSpawn()
      end

      -- 当迪拜机场被占领，向迪拜开火的火炮开始重生
      if IRONBLOODARTYs[3] then
        if ARTY_SCHEDULERS[3] then
          ARTY_SCHEDULERS[3].object:Start(ARTY_SCHEDULERS[3].id)
        end
        IRONBLOODARTYs[3]:SpawnScheduleStart()
      end
    elseif tempCoalition == SIDE.RED then
      Flag_Dubai = false
      -- 关闭机场
      clients_Dubai:ForEachClient(clientsControl, { val = 100 })
      MessageToAll("格里芬战情通报: 迪拜国际机场已经被铁血夺回, 请重新规划进攻以占领机场",
        10, "格里芬战情通报")
      if IRONBLOOD_AIRBASE_ATTACK_Dubai then
        IRONBLOOD_AIRBASE_ATTACK_Dubai:SpawnScheduleStop()
      end

      -- 生成红方基地
      if IRONBLOODBASE_Dubai:GetFirstAliveGroup() == nil then
        IRONBLOODBASE_Dubai:ReSpawn()
      end

      -- 进攻迪拜的火炮结束
      if ARTY_SCHEDULERS[3] then
        ARTY_SCHEDULERS[3].object:Stop(ARTY_SCHEDULERS[3].id)
      end
    end
  end
end

function ZoneCaptureCoalition_Dubai:OnEnterAttacked() -- 通报正在被进攻
  local beattacked = self:GetCoalition()
  if beattacked == SIDE.BLUE then
    MessageToAll("格里芬战情通报: 迪拜正在被铁血进攻！")
    SOUND_HOSTILE_01:ToAll()
  else
    MessageToAll("格里芬战情通报: 格里芬地面部队正在进攻迪拜。")
    SOUND_BASEATTACKING:ToAll()
  end
end

function ZoneCaptureCoalition_Dubai:onenterEmpty() --当玩家清除完目标后生成蓝方以占领
  if not Flag_Dubai then
    MessageToAll("格里芬战情通报: 迪拜威胁已被清除。正在接管", 10, "格里芬战情通报")
    GRIFFINBASE_Dubai:ReSpawn()
  else
    IRONBLOODBASE_Dubai:ReSpawn()
  end
end

_SETTINGS:SetPlayerMenuOff()
env.setErrorMessageBoxEnabled(false)
